#include "algorithm.h"




//计算异或
uint8_t get_chk_xor(uint8_t * da, uint32_t len)
{
	uint8_t xorr;
	uint32_t i;

	xorr = 0;
	for (i = 0; i < len; i++)
	{
		xorr ^= da[i];
	}

	return xorr;
}


//把u32拆分成4字节，大端
void u32toHex4(uint32_t lDa, uint8_t * pDa)
{
	pDa[0] = (uint8_t)(lDa >> 24);
	pDa[1] = (uint8_t)(lDa >> 16);
	pDa[2] = (uint8_t)(lDa >> 8);
	pDa[3] = (uint8_t)(lDa);

}

//把u16拆分成2字节，大端
void u16toHex2(uint32_t lDa, uint8_t * pDa)
{
	pDa[0] = (uint8_t)(lDa >> 8);
	pDa[1] = (uint8_t)(lDa);

}

//把2个u8合并成1个u16
uint16_t hex2CombineOne(uint8_t * da)
{
	return (uint16_t)((0xff00)&(da[0] << 8)) | (da[1] & 0x00ff);
}

//把4个u8合并成1个u32
uint32_t hex4CombineOne(uint8_t * da)
{
	uint32_t reDa;
	reDa = da[0] << 24 |
		da[1] << 16 |
		da[2] << 8 |
		da[3];

	return reDa;
}

//返回a和b的绝对值
uint32_t my_abs(uint32_t a, uint32_t b)
{
	if (a > b)
	{
		return a - b;
	}
	else
	{
		return b - a;
	}
}





	


